package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.WebsitePresentationMapper;
import com.weipai.demo.model.WebsitePresentation;
import com.weipai.demo.service.PresentationService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/28 16:21
 * @Version 1.0
 */
@Service
public class PresentationServiceImpl implements PresentationService {

    private static final Logger log = LoggerFactory.getLogger(PresentationServiceImpl.class);

    @Autowired
    private WebsitePresentationMapper websitePresentationMapper;

    //添加或修改网站介绍
    @Override
    public Response save(String presentation,Integer id) {
        Response response=new Response();
        try {
            WebsitePresentation websitePresentation=new WebsitePresentation();
            websitePresentation.setId(id);
            websitePresentation.setPresentation(presentation);
            //当id为空时,添加
            if (id==null){
                int insert = websitePresentationMapper.insert(websitePresentation);
                response.setCode(RetCode.SAVESUCCESS);
            }else {
                int i = websitePresentationMapper.updateByPrimaryKeySelective(websitePresentation);
                response.setCode(RetCode.UPDATESUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("save"+e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //展示网站介绍
    @Override
    public Response show() {
        Response response=new Response();
        List<WebsitePresentation> websitePresentations = websitePresentationMapper.selectAll();
        response.put("websitePresentations",websitePresentations);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //删除网站介绍
    @Override
    public Response delete(Integer id) {
        Response response=new Response();
        WebsitePresentation websitePresentation=new WebsitePresentation();
        websitePresentation.setId(id);
        if (id!=null){
            int i = websitePresentationMapper.updateByPrimaryKeySelective(websitePresentation);
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.ID_NULL);
        }
        return response;
    }
}
